package com.cgbl.cg.vm.resource

import android.app.Application
import androidx.databinding.ObservableField
import api.ResourceServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseAllTypeBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ResourceMenuActViewModel(application: Application) : GLBaseViewModel(application) {
    val typeName = ObservableField<String>("3D建模")
    var typeId = ""
    val list = arrayListOf<CourseAllTypeBean>()
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val selectType: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val tabDateEvent = SingleLiveEvent<CourseAllTypeBean>()
    }

    val showDialogCommand = BindingCommand<Any>(BindingAction {
        uc.selectType.call()
    })

    fun setTabData(bean: CourseAllTypeBean) {
        typeName.set(bean.name)
        typeId = bean.id ?: ""
        uc.tabDateEvent.postValue(bean)
    }

    fun allType() {
        ResourceServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                    list.clear()
                    list.addAll(t.data)

                    if (list.isNotEmpty()) {
                        setTabData(list[0])
                    }
                }
            })
    }
}